<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box{width:100px;height:100px;background: red;position: absolute;left:0;}
        .line{width:1px;height:500px;position:absolute;left:400px;top:0;background:black;}
    </style>
</head>
<body>
    <div class="box"></div>
    <div class="line"></div>
</body>
<script>

    const obox = document.querySelector(".box");

    let time;

    document.onclick = function(){

        move(obox, "margin", 300)

    }

    function move(ele, attr, target){
        clearInterval(time);
        time = setInterval(()=>{
            // 使用获取非行内样式的方式，替换原有快速获取尺寸类属性的方法，获取元素指定属性的当前值
            let now = parseInt(getStyle(ele, attr));

            let speed = (target - now) / 10;
            speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);

            if(target === now){
                clearInterval(time);
            }else{
                ele.style[attr] = now + speed + "px";
            }
        }, 30);
    }

    // 获取非行内样式的兼容封装
    function getStyle(ele, attr){
        if(getComputedStyle){
            return getComputedStyle(ele)[attr];
        }else{
            return ele.currentStyle[attr];
        }
    }

    
</script>
</html>